package com.yourcompany.service.factory;

import com.yourcompany.api.strategy.LoginStrategy;
import com.yourcompany.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * 登录策略工厂
 */
@Component
public class LoginStrategyFactory {
    // 使用Map存储所有LoginStrategy
    private final Map<String, LoginStrategy> strategyMap;

    // 构造函数，注入所有LoginStrategy
    @Autowired
    public LoginStrategyFactory(List<LoginStrategy> strategies) {
        strategyMap = strategies.stream()
                .collect(Collectors.toMap(LoginStrategy::getLoginType, Function.identity()));
    }
    
    public LoginStrategy getStrategy(String loginType) {
        LoginStrategy strategy = strategyMap.get(loginType);
        if (strategy == null) {
            throw new BusinessException("不支持的登录方式");
        }
        return strategy;
    }
} 